package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.controller.websocket.WebSocketUtils;
import com.csun.cmny.provider.device.csun.camera.CameraMessageHandler;
import com.csun.cmny.provider.device.csun.camera.entity.CsunCameraData;
import com.csun.cmny.provider.model.domain.DmcDeviceAlert;
import com.csun.cmny.provider.model.dto.alert.BatchHandleDeviceAlertDto;
import com.csun.cmny.provider.model.dto.alert.DeviceAlertPageQuery;
import com.csun.cmny.provider.model.dto.alert.HandleDeviceAlertDto;
import com.csun.cmny.provider.model.dto.life.DeviceAlertLifeByIosSelectDto;
import com.csun.cmny.provider.model.entity.IdList;
import com.csun.cmny.provider.model.vo.DeviceReqVo;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.device.DmcDeviceAlertLifeVo;
import com.csun.cmny.provider.model.vo.device.DmcDeviceAlertVo;
import com.csun.cmny.provider.model.vo.life.DmcDeviceByRoom;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.service.impl.JPushService;
import com.csun.cmny.provider.task.RefreshExpireKeyQuartz;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import net.sf.json.JSONObject;
import org.springframework.context.MessageSource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-25 13:29
 * @Description: the class dmc device alert controller
 */
@RestController
@RequestMapping(value = "/alert", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcDeviceAlertController extends BaseController {

    @Resource
    private JPushService jpushService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcDeviceAlertService dmcDeviceAlertService;
    @Resource
    private WebSocketUtils webSocketUtils;
    @Resource
    private OmcRpcService omcRpcService;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private MessageSource messageSource;
    @Resource
    private DmcCsunSleepBindService dmcCsunSleepBindService;
    @Resource
    private DmcCsunNewBodyLocationService dmcCsunNewBodyLocationService;
    @Resource
    private DmcCsunHumidistatLocationService dmcCsunHumidistatLocationService;
    @Resource
    private DmcCsunCameraLocationService dmcCsunCameraLocationService;
    @Resource
    private DmcCsunBodySensorLocationService dmcCsunBodySensorLocationService;
    @Resource
    private RefreshExpireKeyQuartz refreshExpireKeyQuartz;
    @Resource
    private CameraMessageHandler cameraMessageHandler;
    @Resource
    private DmcCsunGatewayLocationService dmcCsunGatewayLocationService;

    /**
     * 查询全部报警信息
     */
//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryDeviceAlertListWithPage", method = RequestMethod.POST)
    public Wrapper queryDeviceAlertListWithPage(@RequestBody DeviceAlertPageQuery deviceAlertPageQuery) {

        deviceAlertPageQuery.setGroupId(getLoginAuthDto().getGroupId());

        PageInfo pageInfo = dmcDeviceAlertService.queryDeviceAlertListWithPage(deviceAlertPageQuery);

        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 根据报警Id删除报警记录
     */
//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteDeviceAlertById(@PathVariable Long id) {

        DmcDeviceAlert dmcDeviceAlertQuery = dmcDeviceAlertService.selectByKey(id);
        Integer result = dmcDeviceAlertService.deleteDeviceAlertById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            dmcDeviceAlertService.deleteDeviceAlertMsg(dmcDeviceAlertQuery);
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 批量删除报警记录
     */
//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/batchDeleteByIdList", method = RequestMethod.POST)
    public Wrapper batchDeleteByIdList(@RequestBody IdList idList) {

        List<DmcDeviceAlert> dmcDeviceAlertList = dmcDeviceAlertService.queryDeviceAlertListByIds(idList.getIdList());
        Integer result = dmcDeviceAlertService.batchDeleteByIdList(idList.getIdList());
        if (result > 0) {
            dmcDeviceAlertService.deleteDeviceAlertListMsg(dmcDeviceAlertList);
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据报警Id查看报警记录
     */
//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/readById/{id}", method = RequestMethod.POST)
    public Wrapper readDeviceAlertById(@PathVariable Long id) {

        DmcDeviceAlert dmcDeviceAlertQuery = dmcDeviceAlertService.selectByKey(id);
        Integer result = dmcDeviceAlertService.readDeviceAlertById(id, getLoginAuthDto());
        if (result > 0) {
            //dmcDeviceAlertService.deleteDeviceAlertMsg(dmcDeviceAlertQuery);
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 批量查看报警记录
     */
//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/batchReadByIdList", method = RequestMethod.POST)
    public Wrapper batchReadByIdList(@RequestBody IdList idList) {

//        List<DmcDeviceAlert> dmcDeviceAlertList = dmcDeviceAlertService.queryDeviceAlertListByIds(idList.getIdList());
        Integer result = dmcDeviceAlertService.batchReadByIdList(idList.getIdList(),
                getLoginAuthDto());
        if (result > 0) {
            //dmcDeviceAlertService.deleteDeviceAlertListMsg(dmcDeviceAlertList);
        }
       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据Id处理报警信息
     */
    @ValidateAnnotation
//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/handleDeviceAlert", method = RequestMethod.POST)
    public Wrapper handleDeviceAlert(@Validated @RequestBody HandleDeviceAlertDto handleDeviceAlertDto,
                                     BindingResult br) {
        DmcDeviceAlert dmcDeviceAlertQuery = dmcDeviceAlertService.selectByKey(handleDeviceAlertDto.getId());
        Integer result = dmcDeviceAlertService.handleDeviceAlert(handleDeviceAlertDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            dmcDeviceAlertService.deleteDeviceAlertMsg(dmcDeviceAlertQuery);
        }

      //  return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 批量处理报警信息
     */
    @RequestMapping(value = "/batchHandleByIdList", method = RequestMethod.POST)
    public Wrapper batchHandleDeviceAlert(@RequestBody BatchHandleDeviceAlertDto batchHandleDeviceAlertDto) {

        List<DmcDeviceAlert> dmcDeviceAlertList = dmcDeviceAlertService
                .queryDeviceAlertListByIds(batchHandleDeviceAlertDto.getIdList());
        Integer result = dmcDeviceAlertService.batchHandleByIdList(batchHandleDeviceAlertDto,
                getLoginAuthDto());
        if (result > 0) {
            dmcDeviceAlertService.deleteDeviceAlertListMsg(dmcDeviceAlertList);
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据楼栋Id和楼层号查询报警信息
     */
    @RequestMapping(value = "/queryDeviceAlertByBuildingIdAndFloorNo", method = RequestMethod.POST)
    public Wrapper queryDeviceAlertByBuildingIdAndFloorNo(Integer buildingId, Integer floorNo) {

        Preconditions.checkArgument(buildingId != null, cmnyMessageSource
                .getMessage("building.id.null", null));
        Preconditions.checkArgument(floorNo != null, cmnyMessageSource
                .getMessage("floor.no.null", null));

        List<DeviceReqVo> deviceReqVoList = (List<DeviceReqVo>) redisTemplate
                .opsForValue().get(RedisKeyUtil.getOmcBuildFloor(buildingId, floorNo));



        if (PublicUtil.isEmpty(deviceReqVoList)) {
            return WrapMapper.ok();
        }

        List<DmcDeviceAlertVo> dmcDeviceAlertVoList = dmcDeviceAlertService.queryDeviceAlertByBuildingIdAndFloorNo(deviceReqVoList);

       // return WrapMapper.ok(dmcDeviceAlertVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),dmcDeviceAlertVoList);
    }

    /**
     * 根据Id更新报警次数
     *
     * @param id    device alert id
     *
     * @return  the wrapper.
     */
//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/updateDeviceAlertPlayTimesById/{id}", method = RequestMethod.POST)
    public Wrapper updateDeviceAlertPlayTimesById(@PathVariable Long id) {

        Preconditions.checkArgument(id != null,  cmnyMessageSource
                .getMessage("device.alert.id.null", null));

        Integer result = dmcDeviceAlertService.updateDeviceAlertPlayTimesById(id);

      //  return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 下发报警信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/sendDeviceAlertMessageById/{id}", method = RequestMethod.POST)
    public Wrapper sendDeviceAlertMessageById(@PathVariable Long id) {

        DmcDeviceAlert dmcDeviceAlert = dmcDeviceAlertService.selectByKey(id);
        if (PublicUtil.isNotEmpty(dmcDeviceAlert)) {
            try {
                // WebSocketServer.sendMessage(JSONObject.fromObject(dmcDeviceAlert).toString(), dmcDeviceAlert.getGroupId() + "");
                webSocketUtils.pushWebsocketMessageWithAlert(dmcDeviceAlert);
            } catch (Exception e) {
                e.printStackTrace();
                return WrapMapper.error();
            }
        }
        return WrapMapper.ok();
        //return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/jpushSendMsg", method = RequestMethod.POST)
    public Wrapper jpushSendMsg(Long id, String tagList) {

        DmcDeviceAlert dmcDeviceAlert = dmcDeviceAlertService.selectByKey(id);
        if (PublicUtil.isNotEmpty(dmcDeviceAlert)) {
            jpushService.sendToAll(cmnyMessageSource.getMessage("device.alert.msg.send", null),
                    cmnyMessageSource.getMessage("device.alert.msg.send", null),
                    dmcDeviceAlert.getAlertInfo(),
                    JSONObject.fromObject(dmcDeviceAlert).toString(),
                    Arrays.asList(tagList.split(",")));
        }

        return WrapMapper.ok();
       //eturn WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/jpushDeleteMsg", method = RequestMethod.POST)
    public Wrapper jpushDeleteMsg(Long id, String tagList) {

        DmcDeviceAlert dmcDeviceAlert = dmcDeviceAlertService.selectByKey(id);
        if (PublicUtil.isNotEmpty(dmcDeviceAlert)) {
            jpushService.sendToAll(cmnyMessageSource.getMessage("device.alert.msg.query", null),
                    cmnyMessageSource.getMessage("device.alert.msg.query", null),
                    dmcDeviceAlert.getAlertInfo(),
                    JSONObject.fromObject(dmcDeviceAlert).toString(),
                    Arrays.asList(tagList.split(",")));
        }

        return WrapMapper.ok();
       //eturn WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/jpushThresholdSaveMsg", method = RequestMethod.POST)
    public Wrapper jpushThresholdSaveMsg(String tagList) {

        Map<String, Object> map = Maps.newHashMap();
        map.put("msgType", "groupThresholdSaveMsg");

        jpushService.sendToAll(cmnyMessageSource.getMessage("group.threshold.save.msg", null),
                cmnyMessageSource.getMessage("group.threshold.save.msg", null),
                cmnyMessageSource.getMessage("group.threshold.save.msg", null),
                JSONObject.fromObject(map).toString(),
                Arrays.asList(tagList.split(",")));

        return WrapMapper.ok();
        //turn WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/jpushBuildingFloorUpdateMsg", method = RequestMethod.POST)
    public Wrapper jpushBuildingFloorUpdateMsg(String tagList) {

        Map<String, Object> map = Maps.newHashMap();
        map.put("msgType", "groupBuildingFloorUpdateMsg");

        jpushService.sendToAll(cmnyMessageSource.getMessage("group.building.floor.update.msg", null),
                cmnyMessageSource.getMessage("group.building.floor.update.msg", null),
                cmnyMessageSource.getMessage("group.building.floor.update.msg", null),
                JSONObject.fromObject(map).toString(),
                Arrays.asList(tagList.split(",")));

        return WrapMapper.ok();
    }

    @RequestMapping(value = "/testAlertSync", method = RequestMethod.POST)
    public Wrapper testAlertSync() {
        String deviceNo = "testDeviceNo";
        DmcDeviceAlert dmcDeviceAlert = new DmcDeviceAlert();

        dmcDeviceAlert.setAlertType(1);
        dmcDeviceAlert.setDeviceNo("test");
        dmcDeviceAlert.setDeviceId(100L);
        dmcDeviceAlert.setOldmanId(1107);
        dmcDeviceAlert.setAlertInfo("这是一个测试的报警");
        dmcDeviceAlert.setOldmanName("testttt");
        dmcDeviceAlert.setLocation("aaaaaa");
        dmcDeviceAlert.setCreatedTime(new Date());

        OldmanBedRoomBuildingVo obrvo = new OldmanBedRoomBuildingVo();
        int ret = dmcDeviceAlertService.csunButtonAlertMsg(deviceNo,dmcDeviceAlert,obrvo);

        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),ret);
    }

    /**
     * 根据老人id查询报警信息
     */
    @RequestMapping(value = "/queryDeviceAlertByOldmanId/{oldmenId}", method = RequestMethod.POST)
    public Wrapper queryDeviceAlertByOldmanId(@PathVariable Integer oldmanId) {

        Preconditions.checkArgument(oldmanId != null, cmnyMessageSource
                .getMessage("oldman.id.null", null));


        List<DmcDeviceAlertVo> dmcDeviceAlertVoList = dmcDeviceAlertService.queryDeviceAlertByOldId(oldmanId);

       // return WrapMapper.ok(dmcDeviceAlertVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),dmcDeviceAlertVoList);
    }


    /*life首页报警信息展示*/
    @RequestMapping(value = "/queryDeviceAlertLife", method = RequestMethod.POST)
    public Wrapper queryDeviceAlertLife() {


        List<DmcDeviceAlertLifeVo> dmcDeviceAlertLifeVos = dmcDeviceAlertService.queryDeviceAlertLife(getLoginAuthDto().getGroupId());
        List<Long> alertIds = new ArrayList<>();
        for (DmcDeviceAlertLifeVo dmcDeviceAlertLifeVo : dmcDeviceAlertLifeVos) {
            //把报警id传进去
            alertIds.add(dmcDeviceAlertLifeVo.getAlertId());
        }
        //给报警信息添加颜色
        if(PublicUtil.isNotEmpty(alertIds)){
            List<DmcDeviceAlertLifeVo> dmcDeviceAlertLifeVoList=dmcDeviceAlertService.selectColor(alertIds,getLoginAuthDto().getGroupId());

            for (DmcDeviceAlertLifeVo dmcDeviceAlertLifeVo : dmcDeviceAlertLifeVos) {
                for (int i = 0; i < dmcDeviceAlertLifeVoList.size(); i++) {
                    if(dmcDeviceAlertLifeVo.getAlertId().equals(dmcDeviceAlertLifeVoList.get(i).getAlertId())){
                        dmcDeviceAlertLifeVo.setColor(dmcDeviceAlertLifeVoList.get(i).getColor());
                    }
                }

            }
        }
        //如果没有存入颜色,则存入默认颜色
        for (DmcDeviceAlertLifeVo dmcDeviceAlertLifeVo : dmcDeviceAlertLifeVos) {
            if(PublicUtil.isEmpty(dmcDeviceAlertLifeVo.getColor())){
                dmcDeviceAlertLifeVo.setColor("rgba(247, 15, 7, 1)");
            }
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), dmcDeviceAlertLifeVos);
        //return WrapMapper.ok(cmnyMessageSource.getMessage("operation.success",null),dmcDeviceAlertLifeVos);

    }

     /*life房间界面报警信息展示*/

//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryDeviceAlertLifeRoom", method = RequestMethod.POST)
    public Wrapper queryDeviceAlertLifeRoom(@RequestBody DeviceAlertPageQuery deviceAlertPageQuery) {

        //deviceAlertPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = dmcDeviceAlertService.queryDeviceAlertLifeRoomIndex(deviceAlertPageQuery);

        return WrapMapper.ok(pageInfo);
    }

    /*life处理报警信息*/
//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/updateAlert/{alertId}", method = RequestMethod.POST)
    public Wrapper updateAlert(@PathVariable Long alertId) {

        DmcDeviceAlert dmcDeviceAlert = dmcDeviceAlertService.selectByKey(alertId);
        if(PublicUtil.isNotEmpty(dmcDeviceAlert.getIsHandle())&&dmcDeviceAlert.getIsHandle()==true){
            return WrapMapper.error(cmnyMessageSource.getMessage("device.alert.handle.ishandle",null));
        }
       /* if(PublicUtil.isNotEmpty(dmcDeviceAlert.getHandleStatus())&&dmcDeviceAlert.getHandleStatus()!=0){
            return WrapMapper.error(cmnyMessageSource.getMessage("device.alert.handle.error",null));
        }*/
        DmcDeviceAlert dmcDeviceAlertQuery = dmcDeviceAlertService.selectByKey(alertId);
        String loginName = getLoginAuthDto().getUserName();

        Integer result = dmcDeviceAlertService.updateAlert(alertId,loginName);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            dmcDeviceAlertService.deleteDeviceAlertMsg(dmcDeviceAlertQuery);

        }

        //  return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
   
    }

//    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/selectRoomIdByDeviceId/{alertId}", method = RequestMethod.POST)
    public Wrapper selectRoomIdByAlertId(@PathVariable Long alertId) {

        //获取每个设备的绑定房间id
        DmcDeviceByRoom bodyRoomIds=dmcCsunBodySensorLocationService.selectRoomIdByDeviceId(alertId);
        DmcDeviceByRoom sleepRoomIds=dmcCsunSleepBindService.selectRoomIdByDeviceId(alertId);
        DmcDeviceByRoom cameraRoomIds=dmcCsunCameraLocationService.selectRoomIdByDeviceId(alertId);
        DmcDeviceByRoom humidistatRoomIds=dmcCsunHumidistatLocationService.selectRoomIdByDeviceId(alertId);
        DmcDeviceByRoom newBodyRoomIds=dmcCsunNewBodyLocationService.selectRoomIdByDeviceId(alertId);
        DmcDeviceByRoom gatewayRoomIds=dmcCsunGatewayLocationService.selectRoomIdByDeviceId(alertId);

        if(bodyRoomIds!=null){

            return WrapMapper.ok(bodyRoomIds.getRoomId());
        }
        if(sleepRoomIds!=null){

            return WrapMapper.ok(sleepRoomIds.getRoomId());
        }
        if(cameraRoomIds!=null){

            return WrapMapper.ok(cameraRoomIds.getRoomId());
        }
        if(humidistatRoomIds!=null){

            return WrapMapper.ok(humidistatRoomIds.getRoomId());
        }
        if(newBodyRoomIds!=null){

            return WrapMapper.ok(newBodyRoomIds.getRoomId());
        }
        if(gatewayRoomIds!=null){
            return WrapMapper.ok(gatewayRoomIds.getRoomId());
        }

        return WrapMapper.ok();
    }

    /*life批量处理报警信息*/
    @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/updateAlerts", method = RequestMethod.POST)
    public Wrapper updateAlerts(@RequestBody IdList alertIds) {

        List<Long> idList = alertIds.getIdList();
        int a=0;
        String loginName = getLoginAuthDto().getUserName();


        for (int i = 0; i < idList.size(); i++) {
            DmcDeviceAlert dmcDeviceAlert = dmcDeviceAlertService.selectByKey(idList.get(i));
            if (PublicUtil.isNotEmpty(dmcDeviceAlert.getIsHandle()) && dmcDeviceAlert.getIsHandle() == true) {
                idList.remove(i);
                a++;
            }
        }
        List<DmcDeviceAlert> dmcDeviceAlertList = dmcDeviceAlertService.queryDeviceAlertListByIds(idList);

        Integer result=dmcDeviceAlertService.updateAlerts(idList, loginName);
        if(a>0){
            return WrapMapper.error(cmnyMessageSource.getMessage("device.alert.handle.ishandle", null));
        }

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            dmcDeviceAlertService.deleteDeviceAlertListMsg(dmcDeviceAlertList);

        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    @RequestMapping(value = "/nightAlertTest/{time}", method = RequestMethod.POST)
    public Wrapper updateAlerts(@PathVariable String time) {
        refreshExpireKeyQuartz.checkOldmanInRoomAtNight(time);
        return null;
    }

    @RequestMapping(value = "/cameraNightAlertTest", method = RequestMethod.POST)
    public Wrapper cameraNightAlertTest() {
        CsunCameraData csunCameraData = (CsunCameraData)redisTemplate.opsForValue().get(RedisKeyUtil.getCsunCameraKey("camera-005"));
        cameraMessageHandler.dealCsunCameraHomeMsg(csunCameraData, "{\"msgType\":\"cameraHomeMsg\",\"sn\":\"camera-005\",\"taskId\":\"1079109791\",\"homeMsg\":\"out\"}");
        return null;
    }

    /*life-iOS 首页报警信息展示*/
    @RequestMapping(value = "/queryDeviceAlertLifeByIos", method = RequestMethod.POST)
    public Wrapper queryDeviceAlertLifeByIos(@RequestBody DeviceAlertLifeByIosSelectDto deviceAlertLifeByIosSelectDto) {


        deviceAlertLifeByIosSelectDto.setGroupId(getLoginAuthDto().getGroupId());
        List<DmcDeviceAlertLifeVo> dmcDeviceAlertLifeVos = dmcDeviceAlertService.queryDeviceAlertLifeByIos(deviceAlertLifeByIosSelectDto);
        List<Long> alertIds = new ArrayList<>();
        for (DmcDeviceAlertLifeVo dmcDeviceAlertLifeVo : dmcDeviceAlertLifeVos) {
            //把报警id传进去
            alertIds.add(dmcDeviceAlertLifeVo.getAlertId());
        }
        //给报警信息添加颜色
        if(PublicUtil.isNotEmpty(alertIds)){
            List<DmcDeviceAlertLifeVo> dmcDeviceAlertLifeVoList=dmcDeviceAlertService.selectColor(alertIds,getLoginAuthDto().getGroupId());

            for (DmcDeviceAlertLifeVo dmcDeviceAlertLifeVo : dmcDeviceAlertLifeVos) {
                for (int i = 0; i < dmcDeviceAlertLifeVoList.size(); i++) {
                    if(dmcDeviceAlertLifeVo.getAlertId().equals(dmcDeviceAlertLifeVoList.get(i).getAlertId())){
                        dmcDeviceAlertLifeVo.setColor(dmcDeviceAlertLifeVoList.get(i).getColor());
                    }
                }

            }
        }
        //如果没有存入颜色,则存入默认颜色
        for (DmcDeviceAlertLifeVo dmcDeviceAlertLifeVo : dmcDeviceAlertLifeVos) {
            if(PublicUtil.isEmpty(dmcDeviceAlertLifeVo.getColor())){
                dmcDeviceAlertLifeVo.setColor("rgba(247, 15, 7, 1)");
            }
        }

        PageInfo<DmcDeviceAlertLifeVo> pageInfo = new PageInfo<>(dmcDeviceAlertLifeVos);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);

    }
}
